﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DienLib.PopUp
{
    /// <summary>
    /// Interaction logic for PopUpOK.xaml
    /// </summary>
    public partial class PopUpWatch : UserControl
    {
        PopUpResult _result = PopUpResult.NA;
        private PopUpWatch(string Comment, string Title, object parseObj)
        {
            InitializeComponent();
            treeList1.Model = new WatchModel(parseObj);
            Run run = new Run(textBlock1.Text); run.Foreground = Brushes.Black; textBlock1.Inlines.Clear(); textBlock1.Inlines.Add(new Bold(run));
            textBlock2.Inlines.Clear();
            textBlock1.Inlines.AddRange(Command.HighlightTextBlockMatch(Title, "\"[^\"]*\"", null, true)); // "[^"]*" : Mean "*" any character with double-quotes
            textBlock2.Inlines.AddRange(Command.HighlightTextBlockMatch(Comment, "\"[^\"]*\"", System.Windows.Media.Brushes.Blue, true));
            //if (items != null) treeView1.ItemsSource = items;
        }

        /// <summary>
        /// Additional Method from User to Get Value back from PopUp
        /// </summary>
        /// <returns>String contain PopUp Result</returns>
        public static PopUpResult ShowDialog(string Comment, string Title = "N/A", object parseObj = null)
        {
            PopUpWatch popup = new PopUpWatch(Comment, Title, parseObj);
            Window window = new Window();
            window.Content = popup;
            window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            window.WindowStyle = System.Windows.WindowStyle.None;
            window.AllowsTransparency = true;
            window.Background = System.Windows.Media.Brushes.Transparent;
            window.Topmost = true;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.MouseLeftButtonDown += Window_MouseLeftButtonDown;
            System.Media.SystemSounds.Exclamation.Play();

            /*string fileName;

            fileName = Environment.CurrentDirectory + @"\Dictionaries\" + "TreeStyles.xaml";

            if (System.IO.File.Exists(fileName))
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
                {
                    // Read in ResourceDictionary File
                    ResourceDictionary dic =
                       (ResourceDictionary)System.Windows.Markup.XamlReader.Load(fs);
                    
                    // Clear any previous dictionaries loaded
                    //window.Resources.MergedDictionaries.Clear();
                    
                    // Add in newly loaded Resource Dictionary
                    window.Resources.MergedDictionaries.Add(dic);
                }
            }*/
                /*<ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Luna;V3.0.0.0;31bf3856ad364e35;component\themes/luna.normalcolor.xaml" />
                <ResourceDictionary Source="TreeStyles.xaml" />
                </ResourceDictionary.MergedDictionaries>
                </ResourceDictionary>*/
            window.ShowDialog(); // ShowDialog can be used Internal ? HA HA HA ! Any Side Effect ?
            window.MouseLeftButtonDown -= Window_MouseLeftButtonDown;
            return popup._result;
        }

        // Su dung Dequy de parse tat ca cac objects
        public static void getPublicProperty(object prop, TreeViewItem item)
        {
            if (prop.GetType() == typeof(System.Reflection.PropertyInfo))
            {
                System.Reflection.PropertyInfo[] pinfos = (prop as System.Reflection.PropertyInfo).PropertyType.GetProperties();
                foreach (System.Reflection.PropertyInfo pinfo in pinfos)
                {
                    TreeViewItem it = new TreeViewItem(); it.Header = pinfo.Name;
                    getPublicProperty(pinfo, it);
                    item.Items.Add(it);
                }
            }
            else
            {   // object not PropertyInfo
                System.Reflection.PropertyInfo[] pinfos = prop.GetType().GetProperties();
                foreach (System.Reflection.PropertyInfo pinfo in pinfos)
                {
                    TreeViewItem it = new TreeViewItem(); it.Header = pinfo.Name;
                    getPublicProperty(pinfo, it);
                    item.Items.Add(it);
                }
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this._result = PopUpResult.OK;
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private static void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            (sender as System.Windows.Window).DragMove();
        }
    }
}
